﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BDSimpleStatus.Models;
using System.Threading.Tasks;

namespace BDSimpleStatus.Controllers
{
    public class StatusController : Controller
    {
        // This controller will probably always have one action. 
        // /status to show the servers status'
        // GET: /Status/

        private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();


        public ActionResult Index()
        {

            if (Properties.Settings.Default.RunChecksOnStatusLoad)
            {
                // Call /bdssapi/run url
                string zurl = Request.ServerVariables["PATH_INFO"];
                string url = "http://" + Request.ServerVariables["SERVER_NAME"] + ":"
                    + Request.ServerVariables["SERVER_PORT"] 
                    + zurl.Substring(0, zurl.LastIndexOf("/") + 1) + "bdssapi/run";
                logger.Info("URL Created for hitting api/run is {0}", url);
                var task = Task.Factory.StartNew(() =>
                    {
                        Checks.RunChecksHttp(url);
                    });
            }
            BdssRepository repo = new BdssRepository();
            StatusFormViewModel sfvm = new StatusFormViewModel(repo.GetServerCheckModelList());
            return View(sfvm);
        }

    }

    public class StatusFormViewModel
    {

        //public List<ServerCheckModel> scml { get; set; }
        public List<ServerCheckFormViewModel> scfvm { get; set; }


        public StatusFormViewModel(List<ServerCheckModel> model)
        {
            //scml = model;
            scfvm = new List<ServerCheckFormViewModel>();
            foreach (ServerCheckModel scm in model)
            {
                scfvm.Add(new ServerCheckFormViewModel(scm));

            }

        }
    }
}
